const user = {
  state: {
    accessToken: ''
  },
  mutations: {
    logout (state, vm) {
      // 清空state
      for (let key in state) {
        delete state[key]
      }

      state.accessToken = '' // 恢复为默认，否则再次登录保存
    },
    setUserInfo (state, info) {
      console.log(info)
      // 设置用户登录信息
      if (!info) { return }
      state.userName = info.user_name
      state.userAccount = info.user_account
      state.globalName = info.global_name
      state.userId = info.user_id
      state.clientSecret = info.client_secret
      state.expiresAt = info.expires_at
      state.socketIp = info.socket_ip
      state.lsatLoginDate = info.last_login_date
      state.ipAddress = info.ip_address
      state.userPicUrl = info.user_pic_url

      state.companyId = info.company_id
      state.companyName = info.company_name
      state.comManagerId = info.com_manager_id
      state.comManagerName = info.com_manager_name
      state.comOtherManagerIds = info.com_other_manager_ids
      state.comOtherManagerNames = info.com_other_manager_names

      state.deptId = info.dept_id
      state.deptName = info.dept_name
      state.deptManagerId = info.dept_manager_id
      state.deptManagerName = info.dept_manager_name
      state.deptOtherManagerIds = info.dept_other_manager_ids
      state.deptOtherManagerNames = info.dept_other_manager_names

      if (info.access_token !== 'null' && info.access_token !== null &&
                info.access_token !== 'undefined' && info.access_token !== undefined) {
        state.accessToken = info.access_token
      }

      // localStorage直接使用会被ESLint提示未定义
      window.localStorage.setItem('loginData', JSON.stringify(info))
      window.localStorage.setItem('accessToken', info.access_token)
    },
    updateCompanyInfo (state, info) {
      // 更新公司信息
      state.companyName = info.name
      state.comOtherManagerIds = info.other_manager_ids
      state.comOtherManagerNames = info.other_manager_names

      let loginData = JSON.parse(window.localStorage.getItem('loginData'))
      loginData.company_name = info.name
      loginData.com_other_manager_ids = info.other_manager_ids
      loginData.com_other_manager_names = info.other_manager_names

      window.localStorage.setItem('loginData', JSON.stringify(loginData))
    }
  }
}

export default user
